package com.jnu.keepaccounts.manage;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jnu.keepaccounts.R;
import com.jnu.keepaccounts.data.XuanKa;
import com.jnu.keepaccounts.data.XuanKaBank;
import com.jnu.keepaccounts.record.ShouZhiFragment;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link YFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class YFragment extends Fragment {

    private int flag= 0;
    private ArrayList<XuanKa> delete_xuanKas = new ArrayList<>();
    private ArrayList<XuanKa> xuanKas = new ArrayList<>();
    private MyAdapter myAdapter;
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public YFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment YFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static YFragment newInstance(String param1, String param2) {
        YFragment fragment = new YFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_y, container, false);

        this.xuanKas = getArguments().getParcelableArrayList("xuanKas");
        RecyclerView recyclerView = view.findViewById(R.id.y_fragment_recyclerView);

        Button add_button = view.findViewById(R.id.add_button);

        View view1 = inflater.inflate(R.layout.activity_manage, container, false);
        Button delete = view1.findViewById(R.id.delete_or_add);
        delete.setOnClickListener(v -> {
            if(flag == 0){
                flag = 1;
                delete.setText("完成");
            }
            else {
                AlertDialog dialog;
                dialog = new AlertDialog.Builder(getContext())
                        .setTitle("删除")  //设置标题
                        .setMessage("删除：将会直接删除该分类以及分类下的记录，不可恢复。\n\n禁用：把分类设置未未启用，添加记录页面将不在显示该分类，您可随时恢复。\n\n您确定要继续吗？") //提示信息
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                for(XuanKa xuanKa:delete_xuanKas){
                                    xuanKas.remove(xuanKa);
                                }
                                XuanKaBank xuanKaBank = new XuanKaBank(getContext());
                                xuanKaBank.saveData();
                                myAdapter.notifyDataSetChanged();
                            }
                        })   //添加“确定”按钮
                        .setNegativeButton("取消",null)   //添加“取消”按钮
                        .create();  //创建对话框
                dialog.show();  //显示对话框
                flag = 0;
                delete.setText("多选删除");
            }
        });
        GridLayoutManager layoutManager = new GridLayoutManager(getContext(),5);
        recyclerView.setLayoutManager(layoutManager);
        myAdapter = new MyAdapter(xuanKas);
        recyclerView.setAdapter(myAdapter);

        return view;
    }
    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{
        private ArrayList<XuanKa> xuanKas;
        public MyAdapter(ArrayList<XuanKa> xuanKas){
            this.xuanKas = xuanKas;
        }
        @NonNull
        @Override
        public MyAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.xuanka_holder,parent,false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull MyAdapter.ViewHolder holder, int position) {
            XuanKa xuanKa = xuanKas.get(position);
            holder.xuanka.setBackgroundResource(xuanKa.getPictureId());
            holder.textView.setText(xuanKa.getName());

        }

        @Override
        public int getItemCount() {
            return xuanKas.size();
        }
        public class ViewHolder extends RecyclerView.ViewHolder{
            ImageView xuanka;
            TextView textView;
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                xuanka = itemView.findViewById(R.id.xuanka_name);
                textView = itemView.findViewById(R.id.text_name);
                itemView.setOnClickListener(v -> {
                    if(flag == 0){
//                        Toast.makeText(getContext(),"aaa",Toast.LENGTH_SHORT).show();
                        int position = getAdapterPosition();
                        Intent intent = new Intent(getContext(),Show.class);
                        intent.putExtra("name",xuanKas.get(position).getName());
                        intent.putExtra("pictureId",xuanKas.get(position).getPictureId());
                        startActivity(intent);
                    }
                    else {
                        if(delete_xuanKas.contains(xuanKas.get(getAdapterPosition()))){
                            delete_xuanKas.remove(xuanKas.get(getAdapterPosition()));
                        }
                        else{
                            delete_xuanKas.add(xuanKas.get(getAdapterPosition()));
                        }
                    }
                });
            }
        }
    }
}